package com.zkingsoft.web.action;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.common.tools.ServiceUtil;
import com.zkingsoft.web.bean.ProductAttrDef;
import com.zkingsoft.web.dao.ProductAttrDefDao;
import com.zkingsoft.web.tool.Tools;

/**
 * @description (产品属性定义)
 * @author 姜友瑶
 * @date 2018-12-12 13:44
 */
@Controller
@RequestMapping(value = "admin/productAttrDef")
public class ProductAttrDefAction {

	@Autowired
	private ProductAttrDefDao productAttrDefDao;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "ProductAttrDef_BEV";

	@Autowired
	private ServiceUtil serviceUtil;

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(ProductAttrDef productAttrDef, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		productAttrDef.setCompanyId(user.getCompanyId());
		List<ProductAttrDef> dataList = productAttrDefDao.selectInPage(productAttrDef, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList,
				productAttrDefDao.selectTotalRecord(productAttrDef));
		return result;
	}

	/**
	 * 展示所有属性
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年12月12日
	 * @param productAttrDef
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult showList(ProductAttrDef productAttrDef) {
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		productAttrDef.setCompanyId(user.getCompanyId());
		List<ProductAttrDef> dataList = productAttrDefDao.selectByModel(productAttrDef);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList);
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/addProductAttrDef")
	public @ResponseBody AjaxResult addProductAttrDef(ProductAttrDef productAttrDef) {
		String companyId=Tools.getCompanyId();
		if (StringUtils.isNotBlank(productAttrDef.getAttrCode())) {
			if (serviceUtil.addCheckRepeatTowColumn("product_attr_def", "attr_code", productAttrDef.getAttrCode(),"company_id",companyId)) {
				throw new GlobleException(SystemErrorCode.DATA_REPEAT, productAttrDef.getAttrCode());
			}
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		productAttrDef.setId(UUIDUtil.getRandomID());
		productAttrDef.setCreateBy(user.getSuName());
		productAttrDef.setUpdateBy(user.getSuName());
		productAttrDef.setCompanyId(user.getCompanyId());
		int i = productAttrDefDao.insert(productAttrDef);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "(产品属性定义)");
		} else {
			throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
		}
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modifyProductAttrDef")
	public @ResponseBody AjaxResult modifyProductAttrDef(ProductAttrDef newProductAttrDef) {
		String companyId=Tools.getCompanyId();
		ProductAttrDef oldProductAttrDef = WebUtil.getSessionAttribute(BEV);
		if (StringUtils.isNotBlank(newProductAttrDef.getAttrCode())) {
			if (serviceUtil.updateCheckRepeatTowColumn("product_attr_def", "attr_code", newProductAttrDef.getAttrCode(),
					"company_id",companyId,
					"attr_code", oldProductAttrDef.getAttrCode())) {
				throw new GlobleException(SystemErrorCode.DATA_REPEAT, newProductAttrDef.getAttrCode());
			}
		}
		// 自己不是是自己的父类
		if (StringUtils.equalsIgnoreCase(newProductAttrDef.getId(), newProductAttrDef.getParentId())) {
			return new AjaxResult(AjaxResult.STATUS_FAIL, "上级属性不能为本身");
		}

		int i = 0;
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldProductAttrDef, newProductAttrDef)) {
				i = MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldProductAttrDef, newProductAttrDef);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newProductAttrDef);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("id", oldProductAttrDef.getId());
			productAttrDefDao.updateByMap(modifyMap);
		}
		i = MatrixConstance.DML_SUCCESSS;
		WebUtil.removeSessionAttribute(BEV);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, "(产品属性定义)");
		} else {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(String id) {
		ProductAttrDef productAttrDef;
		if (id != null) {
			productAttrDef = productAttrDefDao.selectById(id);
			WebUtil.getRequest().setAttribute("obj", productAttrDef);
			WebUtil.setSessionAttribute(BEV, productAttrDef);
		}
		return "admin/web/ProductAttrDef-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<String> ids = StringUtils.strToCollToString(keys, ",");
		int i = productAttrDefDao.deleteByIds(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, i);
		} else {
			throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL);
		}
	}

}